
#ifndef _SCOPED_FD_H_20180919_
#define _SCOPED_FD_H_20180919_

#include "smart_fd_inner.hpp"

class ScopedFd
{
  public:
    explicit ScopedFd(int fd, SmartFd::Deleter deleter = Bind(SmartFd::DefaultClose, _1))
        : fd_(fd), deleter_(deleter) {}

    ~ScopedFd() {
        reset();
    }

    void reset() {
        if (fd_ >= 0) {
            deleter_(fd_);
            fd_ = -1;
        }
    }

    operator int () { return fd_; }

  private:
    ScopedFd(const ScopedFd &);
    ScopedFd& operator = (const ScopedFd &);

    int fd_;
    SmartFd::Deleter deleter_;
};

#endif //_SCOPED_FD_H_20180919_
